﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Practices.Prism.Events;
using System.Reactive.Linq;
using System.Reactive.Subjects;
using System.Reactive.Concurrency;
using System.Reactive;

namespace Codeplex.KinkumaFramework
{
    public static class EventAggregatorExtensions
    {
        public static void Publish<TPayload>(this IEventAggregator self, TPayload payload)
        {
            self.GetEvent<CompositePresentationEvent<TPayload>>()
                .Publish(payload);
        }

        public static IObservable<TPayload> ToObservable<TPayload>(this IEventAggregator self)
        {
            var subject = new Subject<TPayload>();
            self.GetEvent<CompositePresentationEvent<TPayload>>()
                .Subscribe(subject.OnNext);
            return subject.AsObservable();
        }

        public static IDisposable Subscribe<TPayload>(this IObservable<TPayload> source, IEventAggregator subscriber)
        {
            return source.Subscribe(payload => subscriber.Publish(payload));
        }
    }
}
